<?php
    $q = $_GET["q"];
    if ($q == 0) {
        //文件上传需传输一个openid和商家名称（business）
        $a = mysqli_connect("localhost","root","gala123456");
        $db = mysqli_select_db($a,"sheep");
        $str_all = "";
        $nei9 = "";
        $sql9 = "SELECT `sheep_url` FROM `sheep` WHERE `sheepID` = '".$_POST["id"]."'";
        $query9 = mysqli_query($a,$sql9);
        while($row9 = mysqli_fetch_row($query9)){
            foreach($row9 as $data9){
                $nei9 = $data9;
            }
        }
        if ($_POST["num"] == 1) {
            $sql2 = "UPDATE `sheep` SET `sheep_url` = '".$_FILES["file"]["name"]."' WHERE `sheepID` = '".$_POST["id"]."'";
            $query2 = mysqli_query($a,$sql2);
        } else if ($_POST["num"] == 2 or $_POST["num"] == 3 or $_POST["num"] == 4) {
            $str2 = $nei9."-".$_FILES["file"]["name"];
            $sql2 = "UPDATE `sheep` SET `sheep_url` = '".$str2."' WHERE `sheepID` = '".$_POST["id"]."'";
            $query2 = mysqli_query($a,$sql2);
        }
        $sql4 = "SELECT `sheepID` FROM `sheep` WHERE `business` = '".$_POST["business"]."'";
        $query4 = mysqli_query($a,$sql4);
        $business_str = "";
        $id_arr = array();
        while($row4 = mysqli_fetch_row($query4)){
            foreach($row4 as $data4){
                array_push($id_arr,$data4);
            }
        }
        for ($i = 0,$j = 0;$i < 2 * count($id_arr)-1;$i++) {
            if ($i % 2 == 0) {
                $business_str = $business_str.$id_arr[$j];
                $j++;
            }else if ($i % 2 == 1) {
                $business_str = $business_str."-";
            }
        }
        $sql5 = "UPDATE `business` SET `business_sheepID` = '$business_str' WHERE `business_name` = '".$_POST["business"]."'";
        $query5 = mysqli_query($a,$sql5);
        // 上传文件
        move_uploaded_file($_FILES["file"]["tmp_name"], "sheep_img/" .$_FILES["file"]["name"]);
    }else if ($q == 1) {
        $class = $_GET["class"];
        $url = $_GET["url"];
        $date = $_GET["date"];
        $weight = $_GET["weight"];
        $openid = $_GET["openid"];
        $arr = array($class,$url,$date,$weight);
        $a = mysqli_connect("localhost","root","gala123456");
        $db = mysqli_select_db($a,"sheep");
        $sql = "SELECT * FROM `sheep`";
        $query = mysqli_query($a,$sql);
        $ji = 0;
        while ($row = mysqli_fetch_row($query)) {
            $ji++;
        }
        $sql3 = "SELECT `business_name` FROM `business` WHERE `user_openid` = '$openid'";
        $query3 = mysqli_query($a,$sql3);
        $business = "";
        while($row3 = mysqli_fetch_row($query3)){
            foreach($row3 as $data3){
                $business = $data3;
            }
        }
        $sql8 = "SELECT `ALLID` FROM `sheep` WHERE `business` = '".$business."'";
        $query8 = mysqli_query($a,$sql8);
        $ji2 = 0;
        while ($row8 = mysqli_fetch_row($query8)) {
            $ji2++;
        }
        $sql1 = "INSERT INTO `sheep`(`sheepID`, `sheep_select`, `monitor_url`, `sheep_url`, `sheep_class`, `sheep_birthday`, `sheep_weight`, `business`, `ALLID`) VALUES 
        ('$ji',null,'$url','','$class','$date','$weight','$business','$ji2')";
        $query1 = mysqli_query($a,$sql1);
        $arr = array(
            "fan" => $ji,
            "business" => $business
        );
        echo json_encode($arr);
    }else if ($q == 2) {
        $id = $_GET["id"];
        $a = mysqli_connect("localhost","root","gala123456");
        $db = mysqli_select_db($a,"sheep");
        $sql6 = "DELETE FROM `sheep` WHERE `sheepID` = '$id'";
        $query6 = mysqli_query($a,$sql6);
        $sql3 = "SELECT `business_name` FROM `business` WHERE `user_openid` = '".$_GET["openid"]."'";
        $query3 = mysqli_query($a,$sql3);
        $name = "";
        while ($row3 = mysqli_fetch_row($query3)) {
            foreach ($row3 as $data3) {
                $name = $data3;
            }
        }
        $sql4 = "SELECT `sheepID` FROM `sheep` WHERE `business` = '$name'";
        $query4 = mysqli_query($a,$sql4);
        $business_str = "";
        $id_arr = array();
        while($row4 = mysqli_fetch_row($query4)){
            foreach($row4 as $data4){
                array_push($id_arr,$data4);
            }
        }
        for ($i = 0,$j = 0;$i < 2 * count($id_arr)-1;$i++) {
            if ($i % 2 == 0) {
                $business_str = $business_str.$id_arr[$j];
                $j++;
            }else if ($i % 2 == 1) {
                $business_str = $business_str."-";
            }
        }
        $sql5 = "UPDATE `business` SET `business_sheepID` = '$business_str' WHERE `business_name` = '$name'";
        $query5 = mysqli_query($a,$sql5);
    }else if ($q == 3) {
        $arr = array();
        $a = mysqli_connect("localhost","root","gala123456");
        $db = mysqli_select_db($a,"sheep");
        $sql0 = "SELECT `business_name` FROM `business` WHERE `user_openid` = '".$_GET["openid"]."'";
        $query0 = mysqli_query($a,$sql0);
        $name = "";
        while ($row0 = mysqli_fetch_row($query0)) {
            foreach ($row0 as $data0) {
                $name = $data0;
            }
        }
        $sql = "SELECT `sheepID` FROM `sheep` WHERE `business` = '$name'";
        $query = mysqli_query($a,$sql);
        while ($row = mysqli_fetch_row($query)) {
            foreach ($row as $data) {
                array_push($arr,$data);
            }
        }
        echo json_encode($arr);
    }else if ($q == 4) {
        $a = mysqli_connect("localhost","root","gala123456");
        $db = mysqli_select_db($a,"sheep");
        $sql = "SELECT `business_name` FROM `business` WHERE `user_openid` = '".$_GET["openid"]."'";
        $query = mysqli_query($a,$sql);
        $name = "";
        while ($row = mysqli_fetch_row($query)) {
            foreach ($rwo as $data) {
                $name = $data;
            }
        }
        $sql1 = "SELECT `sheep_select`,`ALLID`,`sheep_birthday` FROM `sheep` WHERE `business` = '$nmme'";
        $query1 = mysqli_query($a,$sql1);
        $id_arr = array();
        $birthday_arr = array();
        while ($row1 = mysqli_fetch_row($query1)) {
            $ding = 0;
            $qu = 0;
            foreach ($row1 as $data1) {
                if ($ding == 0) {
                    if ($ding != null) {
                        $qu = 1;
                    }else{
                        break;
                    }
                }
                if ($qu = 1) {
                    if ($ding == 1) {
                        array_push($id_arr,$data1);
                    }else if ($ding == 2) {
                        array_push($birthday_arr,$data1)
                    }
                }
                $ding++;
            }
        }
        $sql2 = "SELECT `pay_state` FROM `orders` WHERE `sheep_id` = '".$_GET["id"]."'"
        $query2 = mysqli_query($a,$sql2);
        while ($row2 = mysqli_fetch_row($query2)) {

        }
    }
?>